﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LiteExpress.Utilities
{
    public class UrlHelper
    {
        public static string Combine(string baseUrl, string url)
        {
            Uri baseUri = new Uri(baseUrl);
            var ret = new Uri(baseUri, url);
            return ret.ToString();
        }


        public static string RemoveQueryInUrl(string url, string key)
        {
            string result = url.Split('?')[0];
            var uri = new Uri(url);
            var queries = HttpUtility.ParseQueryString(uri.Query);
            queries.Remove(key);
            if (queries.Count > 0)
            {
                result = result + "?" + queries.ToString();
            }
            return result;
        }

        public static string SetQueryInUrl(string url, string key, string value)
        {
            string result = url.Split('?')[0];
            var uri = new Uri(url);
            var queries = HttpUtility.ParseQueryString(uri.Query);
            queries[key] = value;
            if (queries.Count > 0)
            {
                result = result + "?" + queries.ToString();
            }
            return result;
        }

    }
}
